home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 247_02 / number.hpp < prev    next >
Text File  |  1989-04-17  |  6KB  |  155 lines

  1. /*
  2.  *    Experimental MIRACL  C++ Header file
  3.  *
  4.  *    AUTHOR  :    M. Scott
  5.  *
  6.  *    NAME    :    number.hpp
  7.  *             
  8.  *    PURPOSE :    Definition of class GFn  (Arithmetic mod n)
  9.  */
  10.  
  11. overload pow;
  12. overload prime;
  13. overload gcd;
  14.  
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include "miracl.h"
  19.  
  20. extern big w14;                        /* use this to store modulus */
  21.  
  22. void normalise(big b)
  23. { /* normalise a number into range */
  24.     divide(b,w14,w14);
  25.     if (size(b)<0) add(b,w14,b);
  26. }
  27.  
  28. class miracl
  29. { /* dummy class to initialise MIRACL */
  30. public:
  31.     miracl(int nd,small nb=MAXBASE) {mirsys(nd,nb);STROUT=TRUE;POINT=TRUE;}
  32. };
  33.  
  34.  
  35. class GFn 
  36.     big fn;
  37. public:
  38.     GFn()                                               { fn = mirvar(0); } 
  39.     GFn(int i)                                          { fn = mirvar(i); }
  40.     GFn(long lg)                         { fn = mirvar(0); lconv(lg,fn);  }
  41.     GFn(GFn& b)                          { fn = mirvar(0); copy(b.fn,fn); }
  42.     GFn(char* s)     { fn = mirvar(0); strcpy(IBUFF,s); cinnum(fn,stdin); }
  43.  
  44.     GFn& operator=(int i)                    {convert(i,fn); return *this;}
  45.     GFn& operator=(const GFn& b)             {copy(b.fn,fn); return *this;}
  46.     GFn& operator=(char* s){strcpy(IBUFF,s);cinnum(fn,stdin);return *this;}
  47.  
  48.     GFn& operator++()           {incr(fn,1,fn);normalise(fn);return *this;}
  49.     GFn& operator--()           {decr(fn,1,fn);normalise(fn);return *this;}
  50.     GFn& operator+=(int i)      {incr(fn,i,fn);normalise(fn);return *this;}
  51.     GFn& operator+=(const GFn& b)
  52.                               {add(fn,b.fn,fn);normalise(fn);return *this;}
  53.  
  54.     GFn& operator-=(int i)     {decr(fn,i,fn);normalise(fn); return *this;}
  55.     GFn& operator-=(const GFn& b)
  56.                         {subtract(fn,b.fn,fn);normalise(fn); return *this;}
  57.  
  58.     GFn& operator*=(int i)  {premult(fn,i,fn);normalise(fn); return *this;}
  59.     GFn& operator*=(const GFn& b){mad(fn,b.fn,fn,w14,w14,fn);return *this;}
  60.  
  61.     GFn& operator/=(int);       
  62.     GFn& operator/=(const GFn&);
  63.  
  64.     friend GFn operator-(const GFn&);
  65.  
  66.     friend GFn operator+(const GFn&,int);
  67.     friend GFn operator+(int,const GFn&);
  68.     friend GFn operator+(const GFn&, const GFn&);
  69.  
  70.     friend GFn operator-(const GFn&, int);
  71.     friend GFn operator-(int,const GFn&);
  72.     friend GFn operator-(const GFn&, const GFn&);
  73.  
  74.     friend GFn operator*(const GFn&, int);
  75.     friend GFn operator*(int,const GFn&);
  76.     friend GFn operator*(const GFn&, const GFn&);
  77.  
  78.     friend GFn operator/(const GFn&, int);
  79.     friend GFn operator/(int,const GFn&);
  80.     friend GFn operator/(const GFn&, const GFn&);
  81.  
  82.     friend bool operator==(const GFn& b1, const GFn& b2)
  83.              {if (compare(b1.fn,b2.fn)==0) return TRUE; else return FALSE;}
  84.     friend bool operator==(const GFn&,int);
  85.     friend bool operator==(int,const GFn&);
  86.  
  87.     friend bool operator!=(const GFn& b1, const GFn& b2)
  88.              {if (compare(b1.fn,b2.fn)!=0) return TRUE; else return FALSE;}
  89.     friend bool operator!=(const GFn&, int);
  90.     friend bool operator!=(int, const GFn&);
  91.  
  92.     friend bool prime(const GFn& b)                   {return prime(b.fn);}
  93.     friend GFn  gcd(const GFn&,const GFn&);
  94.     friend GFn  pow(const GFn&,int);
  95.     friend GFn  pow(const GFn&,const GFn&);
  96.  
  97.     friend void modulo(const GFn& b)      {copy(b.fn,w14); absol(w14,w14);}
  98.  
  99.     friend istream& operator>>(istream& s, GFn& x)
  100.                            { s >> IBUFF; cinnum(x.fn,stdin) ;   return s; }
  101.     friend ostream& operator<<(ostream& s, GFn& x)
  102.                   { cotnum(x.fn,stdout); if (STROUT) s << OBUFF; return s;}
  103.  
  104.     ~GFn()                                             {free((char *)fn); }
  105. };
  106.  
  107.  
  108. GFn& GFn::operator/=(int k)        {GFn z=k; xgcd(z.fn,w14,z.fn,z.fn,z.fn);
  109.                                   mad(z.fn,fn,fn,w14,w14,fn);return *this;}
  110. GFn& GFn::operator/=(const GFn& b)   {GFn z; xgcd(b.fn,w14,z.fn,z.fn,z.fn);
  111.                                   mad(z.fn,fn,fn,w14,w14,fn);return *this;}
  112.  
  113. GFn operator-(const GFn& b)
  114.             {GFn nb; subtract(w14,b.fn,nb.fn);normalise(nb.fn); return nb;}
  115. GFn operator+(const GFn& b,int i)
  116.             {GFn abi; incr(b.fn, i, abi.fn);normalise(abi.fn); return abi;}
  117. GFn operator+(int i,const GFn& b)
  118.             {GFn aib; incr(b.fn, i, aib.fn);normalise(aib.fn); return aib;}
  119. GFn operator+(const GFn& b1, const GFn& b2)
  120.             {GFn abb;add(b1.fn,b2.fn,abb.fn);normalise(abb.fn);return abb;}
  121.  
  122. GFn operator-(const GFn& b, int i)
  123.             {GFn mbi; decr(b.fn, i, mbi.fn);normalise(mbi.fn); return mbi;}
  124. GFn operator-(int i,const GFn& b)   {GFn mib;decr(b.fn,i,mib.fn);
  125.                        negate(mib.fn,mib.fn);normalise(mib.fn);return mib;}
  126. GFn operator-(const GFn& b1, const GFn& b2)
  127.       {GFn mbb;subtract(b1.fn,b2.fn,mbb.fn);normalise(mbb.fn); return mbb;}
  128.  
  129. GFn operator*(const GFn& b, int i)
  130.          {GFn xbi; premult(b.fn, i, xbi.fn);normalise(xbi.fn); return xbi;}
  131. GFn operator*(int i,const GFn& b)
  132.          {GFn xib; premult(b.fn, i, xib.fn);normalise(xib.fn); return xib;}
  133. GFn operator*(const GFn& b1, const GFn& b2)
  134.              {GFn xbb; mad(b1.fn,b2.fn,xbb.fn,w14,w14,xbb.fn); return xbb;}
  135.  
  136. GFn operator/(const GFn& b, int k)  {GFn z=k;xgcd(z.fn,w14,z.fn,z.fn,z.fn);
  137.                               mad(b.fn,z.fn,z.fn,w14,w14,z.fn);  return z;}
  138. GFn operator/(int k,const GFn& b)    {GFn z; xgcd(b.fn,w14,z.fn,z.fn,z.fn);
  139.                        premult(z.fn,k,z.fn);divide(z.fn,w14,w14);return z;}
  140. GFn operator/(const GFn& b1, const GFn& b2)
  141.                                     {GFn z; xgcd(b2.fn,w14,z.fn,z.fn,z.fn);
  142.                              mad(b1.fn,z.fn,z.fn,w14,w14,z.fn);  return z;}
  143.  
  144. GFn gcd(const GFn& b1,const GFn& b2){GFn z;gcd(b1.fn,b2.fn,z.fn);return z;}
  145. GFn pow(const GFn& b,int n)        {GFn z;power(b.fn,n,w14,z.fn);return z;}
  146. GFn pow(const GFn& b1,const GFn& b2)
  147.                              {GFn z;powmod(b1.fn,b2.fn,w14,z.fn);return z;}
  148.  
  149. bool operator==(const GFn& b, int i)              {GFn z=i; return (b==z);}
  150. bool operator==(int i, const GFn& b)              {GFn z=i; return (z==b);}
  151. bool operator!=(const GFn& b, int i)              {GFn z=i; return (b!=z);}
  152. bool operator!=(int i, const GFn& b)              {GFn z=i; return (z!=b);}
  153.  
  154.